/**
 * 
 */
package com.wolfpire.info.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.info.dao.StaffDetailCountInfoDao;
import com.wolfpire.info.model.StaffDetailCountInfo;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.common.util.StringUtils;

/**
 * @author lihd
 *
 */

@Repository("staffDetailCountInfoDao")
public class StaffDetailCountInfoDaoImpl extends
		BaseHibernateDao<StaffDetailCountInfo, Long> implements
		StaffDetailCountInfoDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<StaffDetailCountInfo> list(StaffDetailCountInfo staffDetailCountInfo) {
		Criterion[] criterions = this.createFilter(staffDetailCountInfo);
		Criteria c = this.createCriteria(criterions);
		return c.list();
	}
	
	private Criterion[] createFilter(StaffDetailCountInfo filterStaffDetailCountInfo) {
		if (null == filterStaffDetailCountInfo) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterStaffDetailCountInfo.getId()) {
			criterions.add(Restrictions.eq("id", filterStaffDetailCountInfo.getId()));
		}
		if (null != filterStaffDetailCountInfo.getStaffCountInfoId()) {
			criterions.add(Restrictions.eq("staffCountInfoId", filterStaffDetailCountInfo.getStaffCountInfoId()));
		}
		if (!StringUtils.isBlank(filterStaffDetailCountInfo.getPostKey())) {
			criterions.add(Restrictions.eq("postKey", filterStaffDetailCountInfo.getPostKey()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	@Override
	public void deleteByStaffCountInfoId(Long staffCountInfoId) {
		// TODO Auto-generated method stub
		StringBuffer executeSql = new StringBuffer();
		executeSql.append(" delete from t_staff_detail_count_info where staffcountinfo_id = ?");
		Object[] values = new Object[]{staffCountInfoId};
		this.executeSql(executeSql.toString(), values);
	}

	@Override
	public void batchSave(List<StaffDetailCountInfo> staffDetailCountInfos) {
		// TODO Auto-generated method stub
		if (CollectionUtils.isNotEmpty(staffDetailCountInfos)) {
			Session session = this.getSession();
			for (StaffDetailCountInfo info : staffDetailCountInfos) {
				session.save(info);
			}
			session.flush();
			session.clear();
		}
	}
	
}
